package cn.edu.henu.team.fragments;

import static cn.bmob.v3.Bmob.getApplicationContext;

import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseFragment;
import cn.edu.henu.team.bean.books;
import cn.edu.henu.team.pageplug.MyGridViewAdapter;
import cn.edu.henu.team.ui.readerActivity;

public class fragment1_book extends BaseFragment {
    private static final String TAG = "fragment1_book";
    private EditText et_search;
    private MyGridViewAdapter adapter;


    private ArrayList<books> mData = new ArrayList<>();
    private GridView mBookGridView;
    private SmartRefreshLayout refreshSrl_book;
    public static Fragment newInstance() {
        fragment1_book fragment = new fragment1_book();
        return fragment;
    }

    @Override
    protected void clickEvents() {

    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_1_book;
    }

    @Override
    protected void initView() {
        mBookGridView=mRootView.findViewById(R.id.myBookGrid);
        et_search = mRootView.findViewById(R.id.et_search);
        refreshSrl_book = mRootView.findViewById(R.id.refresh_srl_book);
        refreshSrl_book.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl_book.setRefreshFooter(new ClassicsFooter(getApplicationContext()));


        refreshSrl_book.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl_book.finishRefresh(1000/*,false*/);
                initData();
            }
        });

        refreshSrl_book.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
            }
        });

    }

    @Override
    protected void initData() {
        mData.clear();
        BmobQuery<books> bmobQuery = new BmobQuery("books");
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<books>() {
            @Override
            public void done(List<books> list, BmobException e) {
                if (e==null)
                {
                    if(list!=null)
                    {
                        for (books at:list)
                        {
                            mData.add(at);
                            adapter = new MyGridViewAdapter(mData, getContext(), new MyGridViewAdapter.FilterListener() {
                                @Override
                                public void getFilterData(List<books> list) {
                                    Log.e(TAG, "接口回调成功");
                                    Log.e(TAG, list.toString());
                                    setItemClick(mData);
                                }
                            });
                            mBookGridView.setAdapter(adapter);
                            setListeners();
                        }
                    }
                }else{

                }
            }
        });
    }

    private void setListeners() {
        //没有进行搜索的时候，也要添加对item单击监听
        setItemClick(mData);
        /**
         * 对编辑框添加文本改变监听，搜索的具体功能是在这里实现
         * 文字改变的时候进行搜索，关键方法是重写onTextChanged()方法
         */
        et_search.addTextChangedListener(new TextWatcher() {
            //每次EditText文本改变之前的时候，会回调这个方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //s     输入框中改变前的字符串信息
                //start 输入框中改变前的字符串的起始位置
                //count 输入框中改变前后的字符串改变数量一般为0
                //after 输入框中改变后的字符串与起始位置的偏移量
            }
            //每次EditText文本改变的时候，会回调这个方法
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //第一个参数s 的含义: 输入框中改变后的字符串信息
                //start 输入框中改变后的字符串的起始位置
                //before 输入框中改变前的字符串的位置 默认为0
                //count 输入框中改变后的一共输入字符串的数量
                if (adapter != null) {
                    adapter.getFilter().filter(s);
                }
            }
            //每次EditText文本改变之后的时候，会回调这个方法
            @Override
            public void afterTextChanged(Editable s) {
                //edit  输入结束呈现在输入框中的信息
            }
        });
    }

    private void setItemClick(List<books> filter_list) {
        mBookGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), filter_list.get(position).getName(), Toast.LENGTH_SHORT).show();

                TextView t = view.findViewById(R.id.mybookid);
                Intent it = new Intent(getActivity(), readerActivity.class);
                it.putExtra("Id", t.getText().toString());
                startActivity(it);
            }
        });
    }
}
